package com.starblink.brand.model

import androidx.lifecycle.MutableLiveData
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.brand.bean.BrandAllBean
import com.starblink.brand.bean.BrandAllTitleBean
import com.starblink.brand.bean.BrandItemDecor
import com.starblink.brand.bean.BrandLeftBean
import com.starblink.brand.bean.BrandRightBean
import androidx.lifecycle.viewModelScope
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.android.basic.widget.LoadingDialogManager
import com.starblink.basic.util.ActivityStackManage
import com.starblink.basic.util.BuildConfig
import com.starblink.brand.bean.BrandAlltemBean
import com.starblink.rocketreserver.FetchSubBrandListQuery
import com.starblink.rocketreserver.FetchTopBrandFullListQuery
import com.starblink.rocketreserver.FetchTopBrandSaleListQuery
import com.starblink.rocketreserver.SearchTopBrandQuery
import com.starblink.rocketreserver.fragment.TopBrandInfoVOF
import com.starblink.rocketreserver.type.TopBrandSaleListReq
import com.starblink.rocketreserver.type.TopBrandSearchReq
import kotlinx.coroutines.launch

class BrandAllVM  : BaseViewModel() {
    private val ifMock = BuildConfig.DEBUG && false //设置mock状态
    val topBrandSaleList: MutableLiveData<ArrayList<TopBrandInfoVOF?>> = MutableLiveData(ArrayList())//当前页面品牌数量
    val brandLeftList: ArrayList<Any> = ArrayList()//左边品牌列表
    val brandRightList: ArrayList<Any> = ArrayList()//右边品牌列表

    private val fetchTopBrandFullList:ArrayList<TopBrandInfoVOF?> = ArrayList()
    val brandAllList:MutableLiveData<ArrayList<Any>> = MutableLiveData(ArrayList())// All Brands
    val brandLetterList: MutableLiveData<ArrayList<BrandAllTitleBean>> = MutableLiveData(ArrayList())//字母
    var subscribeList:ArrayList<Any?> =  ArrayList() //订阅品牌列表
    var pageNo = 1
    var hasNext=true //是否有下一页

    private var letterList=arrayListOf(
        BrandAllTitleBean('A'), BrandAllTitleBean('B'),
        BrandAllTitleBean('C'),
        BrandAllTitleBean('D'),
        BrandAllTitleBean('E'), BrandAllTitleBean('F'), BrandAllTitleBean('G'), BrandAllTitleBean('H'),
        BrandAllTitleBean('I'), BrandAllTitleBean('J'), BrandAllTitleBean('K'), BrandAllTitleBean('L'),
        BrandAllTitleBean('M'), BrandAllTitleBean('N'), BrandAllTitleBean('O'), BrandAllTitleBean('P'),
        BrandAllTitleBean('Q'), BrandAllTitleBean('R'), BrandAllTitleBean('S'), BrandAllTitleBean('T'),
        BrandAllTitleBean('U'), BrandAllTitleBean('V'), BrandAllTitleBean('W'), BrandAllTitleBean('X'),
        BrandAllTitleBean('Y'), BrandAllTitleBean('Z'), BrandAllTitleBean('#'))

    //获取折扣 Top 品牌列表
    fun reqFetchTopBrandSaleList(filterType:Int=1,sortType:Int=0,initData: Boolean = true,ifShowLoading:Boolean=false){
        if (initData) {
            pageNo = 1
        }else{
            pageNo++
        }
        viewModelScope.launch {
            val response = SkGqlClient.skQuery(
                FetchTopBrandSaleListQuery(
                    params = TopBrandSaleListReq(
                        pageNo = pageNo,
                        pageSize = 20,
                        filterType = if(filterType==0) 1 else filterType,
                        sortType = sortType
                    )
                ), showDefaultLoading = ifShowLoading
            )
            topBrandSaleList.value?.clear()
            if (initData) {
                brandLeftList.clear()
                brandRightList.clear()
            }
            response?.data?.fetchTopBrandSaleList?.let {fetchTopBrandSaleList->
                fetchTopBrandSaleList.forEach {
                    topBrandSaleList.value?.add(it?.topBrandInfoVOF)
                }
            }

            //Mock 分页
            if(ifMock) {
                if (pageNo < 3) {
                    topBrandSaleList.value?.addAll(reqTestData())
                }
            }

            hasNext= topBrandSaleList.value?.isNotEmpty() == true //是否有下一页
            topBrandSaleList.postValue(topBrandSaleList.value)
        }
    }

    //添加品牌列表
    fun addBrandList(){
        val leftBrandList= topBrandSaleList.value?.mapIndexed { index, topBrandInfoVOF ->
            BrandLeftBean(index==0,topBrandInfoVOF)
        }
        val rightBrandList= topBrandSaleList.value?.mapIndexed { index, topBrandInfoVOF ->
            BrandRightBean(topBrandInfoVOF)
        }
        brandLeftList.addAll(leftBrandList?:ArrayList())
        brandRightList.addAll(rightBrandList?:ArrayList())
        if(!hasNext&&brandRightList.size>0) {
            //没有更多数据
            brandLeftList.add(BrandItemDecor(true))
            brandRightList.add(BrandItemDecor(true))
        }
    }

    //获取全部 Top 品牌列表
    fun reqFetchTopBrandFullList(){
        fetchTopBrandFullList.clear()

        viewModelScope.launch {
            val response = SkGqlClient.skQuery(
                FetchTopBrandFullListQuery(), showDefaultLoading = true
            )
            response?.data?.fetchTopBrandFullList?.let {list->
                list.forEach {
                    fetchTopBrandFullList.add(it?.topBrandInfoVOF)
                }
            }

            //Mock
            if(ifMock) {
                fetchTopBrandFullList.addAll(reqTestData())
            }

            //处理数据
            var rowIndex=0
            letterList.mapIndexed { index, brandAllTitleBean ->
                //A ~ Z 按顺序显示
                var itemBrandList:ArrayList<BrandAlltemBean> = ArrayList()
                val letter=brandAllTitleBean.key
                //当前顺序字母排序
                brandLetterList.value?.add(brandAllTitleBean)

                fetchTopBrandFullList.mapIndexed { index, topBrandInfoVOF ->
                    var key=topBrandInfoVOF?.name?.uppercase()?.toCharArray()?.get(0)?:'#'
                    if(key.code<'A'.code||key.code>'Z'.code){
                        key='#'
                    }
                    if(letter==key){
                        //当前顺序字母排序
                        //字母提取 存在该key
                        brandAllTitleBean.ifEnable=true
                        val lastItem=if(brandAllList.value?.isNotEmpty() == true) brandAllList.value?.last() else null
                        if(lastItem !is BrandAllTitleBean){
                            if(lastItem == null){
                                //第一个item 默认选中该字母
                                brandAllTitleBean.ifChecked=true
                                brandAllList.value?.add(BrandAllTitleBean(key))
                            }
                            if(lastItem is BrandAllBean && lastItem.key!=letter){
                                brandAllList.value?.add(BrandAllTitleBean(key))
                            }
                        }else {}

                        //数据提取
                        if(itemBrandList.size==3){
                            //行最大三个
                            brandAllList.value?.add(BrandAllBean(letter,itemBrandList))
                            itemBrandList=ArrayList()
                        }
                        itemBrandList.add(BrandAlltemBean(rowIndex++,topBrandInfoVOF!!))
                    }
                    if(index==fetchTopBrandFullList.size-1&&itemBrandList.size>0){
                        //不属于这个字母
                        brandAllList.value?.add(BrandAllBean(letter,itemBrandList))
                    }
                }

            }

            brandAllList.value?.add(BrandItemDecor(true))

            brandAllList.postValue(brandAllList.value)
            brandLetterList.postValue(brandLetterList.value)
        }
    }

    //获取用户订阅品牌列表
    fun reqFetchSubBrandList(){
        viewModelScope.launch {
            val response = SkGqlClient.skQuery(
                FetchSubBrandListQuery(), showDefaultLoading = false
            )
            subscribeList.clear()
            response?.data?.fetchSubBrandList?.let { list ->
                subscribeList.addAll(list)
            }

            //mock 数据
            if(ifMock) {
                subscribeList.addAll(reqTestSubBrandList())
            }

            //触发刷新
            brandAllList.postValue(brandAllList.value)
        }
    }

    //搜索 Top 品牌
    fun reqSearchTopBrand(brandName:String,initData: Boolean = true,ifShowLoading:Boolean=false){
        if (initData) {
            pageNo = 1
        }else{
            pageNo++
        }
        viewModelScope.launch {
            val response = SkGqlClient.skQuery(
                SearchTopBrandQuery( param = TopBrandSearchReq(pageNo,20,brandName)  ), showDefaultLoading = ifShowLoading
            )
            topBrandSaleList.value?.clear()
            if (initData) {
                brandLeftList.clear()
                brandRightList.clear()
            }

            response?.data?.searchTopBrand?.let {searchTopBrandList->
                searchTopBrandList.forEach {
                    topBrandSaleList.value?.add(it?.topBrandInfoVOF)
                }
            }

            // Mock
            if(ifMock) {
                if (pageNo < 3) {
                    topBrandSaleList.value?.addAll(reqTestData())
                }
            }

            hasNext= topBrandSaleList.value?.isNotEmpty() == true //是否有下一页

            topBrandSaleList.postValue(topBrandSaleList.value)
        }

    }

    //mock 数据
    private fun reqTestData():ArrayList<TopBrandInfoVOF>{
        val brandRandomList=ArrayList<TopBrandInfoVOF>()
        brandRandomList.add(
            TopBrandInfoVOF(84784,"aaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaabaaaa","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg","https://www.pricerunner.dk/product/640x640/3005268209/Maanesten-Lavania-Ring-Gold.jpg",10,100,false)
        )
        brandRandomList.add(
            TopBrandInfoVOF(84783,"dd","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg",10,100,true)
        )
        brandRandomList.add(
            TopBrandInfoVOF(3732760,"cc","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg",10,100,true)
        )
        brandRandomList.add(
            TopBrandInfoVOF(4,"dd","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg",20,100,false)
        )
        brandRandomList.add(
            TopBrandInfoVOF(5,"1c1","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg",30,100,false)
        )
        brandRandomList.add(
            TopBrandInfoVOF(6,"ea","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg",40,100,false)
        )
        brandRandomList.add(
            TopBrandInfoVOF(7,"es","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg",30,100,false)
        )
        brandRandomList.add(
            TopBrandInfoVOF(8,"eda","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg",30,100,false)
        )
        brandRandomList.add(
            TopBrandInfoVOF(9,"ead","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg",30,100,false)
        )
        brandRandomList.add(
            TopBrandInfoVOF(10,"cdacd","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg",30,100,false)
        )
        brandRandomList.add(
            TopBrandInfoVOF(11,"fdacd","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg",30,100,false)
        )
        brandRandomList.add(
            TopBrandInfoVOF(12,"fad","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg",30,100,false)
        )
        brandRandomList.add(
            TopBrandInfoVOF(13,"cdacd","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg",30,100,false)
        )
        brandRandomList.add(
            TopBrandInfoVOF(14,"cdad","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg",30,100,false)
        )
        brandRandomList.add(
            TopBrandInfoVOF(15,"gad","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg",30,100,false)
        )
        brandRandomList.add(
            TopBrandInfoVOF(16,"rad","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg",30,100,false)
        )
        brandRandomList.add(
            TopBrandInfoVOF(17,"vad","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg","https://www.pricerunner.com/product/640x640/3001984699/Blomus-Modo-Wall-Shelf-51cm.jpg",30,100,false)
        )
        return brandRandomList
    }

    private fun reqTestSubBrandList():ArrayList<Any>{
        val subBrandList=ArrayList<Any>()
        subBrandList.add(2)
        subBrandList.add(6)
        subBrandList.add(8)
        return subBrandList
    }

    //A~# 全部展示
    fun getFilterLetter(){
        var matLetterList:MutableList<BrandAllTitleBean> = mutableListOf()
        letterList.map {
            matLetterList.add(it)
        }
        brandLetterList.value?.addAll(matLetterList)
    }
}